﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;

namespace RedHouse.Data.Base
{
    public class NewsService : INewsService
    {
        #region Declaration
        private NewRedHouseEntitiesDataContext _dataContext;
        #endregion

        private static INewsService _newService;
        public static INewsService Instance
        {
            get
            {
                if (_newService == null)
                {
                    _newService = new NewsService();
                }
                return _newService;
            }
        }
        public NewsService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        #region INewsService Members

        public Entities.RedHouseNews GetNewsById(Guid id)
        {
            New news = _dataContext.News.Where(n => n.ID == id).FirstOrDefault();
            if (news != null)
            {
                RedHouseNews retNews = new RedHouseNews()
                {
                    Id = news.ID,
                    CategoryId = news.CategoryId,
                    Category = CategoryService.Instance.GetCategoryByID(news.CategoryId).Title,
                    CreatedDate = news.CreatedDate,
                    CreatedBy = news.CreatedBy,
                    StartDate = news.StartDate,
                    EndDate = news.EndDate,
                    NewsGroup = news.NewsGroup,
                    State = news.State,
                    LocationId = news.LocationId.HasValue ? news.LocationId.Value : 3,
                    NewsDetail = _dataContext.Details.Where(d => d.AssociatedID == news.ID).FirstOrDefault(),

                };
                retNews.LangId = retNews.NewsDetail.LangID.Value;
                retNews.NewsDetail.Language = null;
                return retNews;
            }

            return null;
        }

        public List<Entities.RedHouseNews> GetAllNewsByGroup(int group)
        {
            List<RedHouseNews> retList = new List<RedHouseNews>();
            List<New> newsList = _dataContext.News.Where(n => n.NewsGroup == group).ToList();
            foreach (New item in newsList)
            {
                RedHouseNews news = new RedHouseNews()
                {
                    Id = item.ID,
                    CreatedDate = item.CreatedDate,
                    CreatedBy = item.CreatedBy,
                    StartDate = item.StartDate,
                    EndDate = item.EndDate,
                    NewsGroup = item.NewsGroup,
                    State = item.State,
                    LocationId = item.LocationId.HasValue ? item.LocationId.Value : 3,
                    Category = _dataContext.Details.Where(d => d.AssociatedID == item.Category.ID).FirstOrDefault().Title,
                    NewsDetail = _dataContext.Details.Where(d => d.AssociatedID == item.ID).FirstOrDefault()
                };
                news.NewsDetail.ShortDesc = news.NewsDetail.Description.Length > 200 ?
                                            string.Format("{0}...", news.NewsDetail.Description.Substring(0, 200)) : news.NewsDetail.Description;
                retList.Add(news);
            }
            return retList;
        }

        public List<Entities.RedHouseNews> GetAllNewsByState(int state)
        {
            List<RedHouseNews> retList = new List<RedHouseNews>();
            List<New> newsList = _dataContext.News.Where(n => n.State == state).ToList();
            foreach (New item in newsList)
            {
                RedHouseNews news = new RedHouseNews()
                {
                    Id = item.ID,
                    CreatedDate = item.CreatedDate,
                    CreatedBy = item.CreatedBy,
                    StartDate = item.StartDate,
                    EndDate = item.EndDate,
                    NewsGroup = item.NewsGroup,
                    State = item.State,
                    LocationId = item.LocationId.HasValue ? item.LocationId.Value : 3,
                    NewsDetail = _dataContext.Details.Where(d => d.AssociatedID == item.ID).FirstOrDefault()
                };
                retList.Add(news);
            }
            return retList;
        }

        public Common.OperationResult SaveNews(Entities.RedHouseNews news)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = news
            };

            try
            {
                New saveNews = new New()
                {
                    ID = news.Id,
                    CreatedDate = news.CreatedDate,
                    CreatedBy = news.CreatedBy,
                    StartDate = news.StartDate,
                    EndDate = news.EndDate,
                    NewsGroup = news.NewsGroup,
                    State = news.State,
                    CategoryId = news.CategoryId,
                    LocationId = news.LocationId
                };

                _dataContext.News.InsertOnSubmit(saveNews);

                if (news.NewsDetail != null)
                {
                    _dataContext.Details.InsertOnSubmit(news.NewsDetail);
                }

                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }

            return result;
        }

        public Common.OperationResult UpdateNews(Entities.RedHouseNews news)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = news
            };

            try
            {
                New saveNews = _dataContext.News.Where(n => n.ID == news.Id).FirstOrDefault();
                if (saveNews != null)
                {
                    saveNews.ID = news.Id;
                    saveNews.CreatedDate = news.CreatedDate;
                    saveNews.CreatedBy = news.CreatedBy;
                    saveNews.StartDate = news.StartDate;
                    saveNews.EndDate = news.EndDate;
                    saveNews.NewsGroup = news.NewsGroup;
                    saveNews.LocationId = news.LocationId;
                    saveNews.State = news.State;
                    saveNews.CategoryId = news.CategoryId;
                }

                if (news.NewsDetail != null)
                {
                    Detail detail = _dataContext.Details.Where(d => d.AssociatedID == news.Id).FirstOrDefault();
                    if (detail != null)
                    {
                        detail.Description = news.NewsDetail.Description;
                        detail.LangID = news.NewsDetail.LangID;
                        detail.Title = news.NewsDetail.Title;
                        detail.ImageUrl = news.NewsDetail.ImageUrl;
                    }
                }

                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }

            return result;
        }

        public Common.OperationResult DeleteNews(Guid id)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = null
            };

            try
            {
                New news = _dataContext.News.Where(n => n.ID == id).FirstOrDefault();
                if (news != null)
                {
                    _dataContext.News.DeleteOnSubmit(news);
                }

                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == id).FirstOrDefault();
                if (detail != null)
                {
                    _dataContext.Details.DeleteOnSubmit(detail);
                }

                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }

            return result;
        }

        #endregion
    }
}
